﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using DataServer.Entities;

namespace DataServer
{
    class DbConnectionmysql
    {
        //本地库
        //private static String connectionString = "host=127.0.0.1;Port=3306;Database=ispay;uid=xmtm;pwd=tianma";
        //private static String connectionString = "host=127.0.0.1;Port=3306;Database=ispay;uid=walker;pwd=yeyizhang";
        //测试库
        //sprivate static String connectionString = "server=172.26.170.21;user id=XMTM; password=tianma; database=f_whms; pooling=false";
        //public MySqlConnection  mysqlconnect = new MySQLConnection(new MySQLConnectionString("localhost", "f_whms", "root", "yeyizhang").AsString);
        public MySqlConnection mysqlconnect=new MySqlConnection(ConstConn.conn);


    }
    class MySqlHander
    {
       public int SqlExcute(string sql)
        {
            int excutenum = 0;
            DbConnectionmysql mysqlconn=new DbConnectionmysql();
            mysqlconn.mysqlconnect.Open();
            MySqlTransaction trans = (MySqlTransaction)mysqlconn.mysqlconnect.BeginTransaction();
            try
            {
                MySqlCommand comm = new MySqlCommand();
                comm.Transaction = trans;
                comm.CommandText = sql;
                excutenum = comm.ExecuteNonQuery();              
                trans.Commit();
                //comm.Dispose();

               
            }
            catch (Exception e)
            {
                trans.Rollback();
                System.Console.Write(e.ToString());
                excutenum= 0;
            }
            finally
            {
                mysqlconn.mysqlconnect.Close();
                
            }
            return excutenum;
           
        }
       public static DataSet SqlSelect(String sql, Dictionary<String, String> dic)
       {
           DataSet ds = new DataSet();
           DbConnectionmysql mysqlconn = new DbConnectionmysql();
           mysqlconn.mysqlconnect.Open();
           MySqlDataAdapter adpt = new MySqlDataAdapter(sql, mysqlconn.mysqlconnect);
           foreach (KeyValuePair<string, string> kvp in dic)
           {

               adpt.SelectCommand.Parameters.Add("?" + kvp.Key, MySqlDbType.String);
               adpt.SelectCommand.Parameters["?" + kvp.Key].Value = kvp.Value;
           }
           try
           {
               adpt.Fill(ds,"Table1");
           }
           catch (Exception e)
           {
               System.Console.Write(e.ToString());
           }
           finally
           {
               mysqlconn.mysqlconnect.Close();
           }
           return ds;
       }
       public static DataSet SqlSelect(String sql)
       {
           DataSet ds = new DataSet();
           DbConnectionmysql mysqlconn = new DbConnectionmysql();
           mysqlconn.mysqlconnect.Open();
           MySqlDataAdapter adpt = new MySqlDataAdapter(sql, mysqlconn.mysqlconnect);           
           try
           {
               adpt.Fill(ds, "Table1");
           }
           catch (Exception e)
           {
               //LogWrite.Log("ERROR",e.ToString());
           }
           finally
           {
               mysqlconn.mysqlconnect.Close();
           }
           return ds;
       }

    }
    
}
